﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;

namespace YJKArch.MapNameTag
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	[Journaling(JournalingMode.NoCommandData)]
	public class Helper
	{
		public static void SetWitdth(TextNote textNote)
		{
			double num = textNote.TextNoteType.get_Parameter(BuiltInParameter.TEXT_SIZE).AsDouble() * 100.0;
			double num2 = textNote.TextNoteType.get_Parameter(BuiltInParameter.TEXT_WIDTH_SCALE).AsDouble();
			byte[] bytes = Encoding.Default.GetBytes(textNote.Text);
			double num3 = num * num2;
			double num4 = (double)bytes.Length * num3 / 1.7;
			textNote.Width = num4 + 1.5;
		}

		public static List<string> GetSystemFamilyElementList<T>(Autodesk.Revit.DB.Document doc)
		{
			List<string> list = new List<string>();
			try
			{
				foreach (Element element in new FilteredElementCollector(doc).OfClass(typeof(T)).ToElements())
				{
					if (element is T)
					{
						list.Add(element.Name);
					}
				}
			}
			catch (Exception ex)
			{
				ex.Message.ToString();
			}
			return list;
		}

		public static Element GetSelectType<T>(Autodesk.Revit.DB.Document doc, string selectTypeStr)
		{
			try
			{
				foreach (Element element in new FilteredElementCollector(doc).OfClass(typeof(T)).ToElements())
				{
					if (element is T && element.Name.StartsWith(selectTypeStr))
					{
						return element;
					}
				}
			}
			catch (Exception ex)
			{
				ex.Message.ToString();
			}
			return null;
		}

		public static void TxtSetNum(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar == ' ')
			{
				e.KeyChar = '\0';
			}
			if (e.KeyChar == '-' && ((System.Windows.Forms.TextBox)sender).Text.Length == 0)
			{
				return;
			}
			if (e.KeyChar > ' ')
			{
				try
				{
					double.Parse(((System.Windows.Forms.TextBox)sender).Text + e.KeyChar.ToString());
				}
				catch
				{
					e.KeyChar = '\0';
				}
			}
		}

		public static void AddCombItems(List<string> levelList, System.Windows.Forms.ComboBox cb)
		{
			if (levelList != null)
			{
				foreach (string item in levelList)
				{
					cb.Items.Add(item);
				}
				if (cb.Items.Count > 0)
				{
					cb.SelectedIndex = 0;
				}
			}
		}

		public static List<string> GetConduitTypeStr(Autodesk.Revit.DB.Document doc, BuiltInCategory category)
		{
			List<string> result;
			try
			{
				List<string> list = new List<string>();
				FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc).OfClass(typeof(ConduitType));
				filteredElementCollector.OfCategory(category);
				foreach (Element element in filteredElementCollector.ToElements())
				{
					list.Add(element.Name);
				}
				result = list;
			}
			catch (Exception ex)
			{
				ex.Message.ToString();
				result = null;
			}
			return result;
		}
	}
}
